<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\cdc;

use app\enum\CdcEnum;
use app\model\mongo\MongoReportModel;
use app\model\mongo\MongoSyncModel;
use app\service\BaseService;
use app\service\cdc\sync\account\AccountLogService;

final class SyncService extends BaseService
{
    /**
     * @var int 媒体id
     */
    protected int $mediaId = 0;

    /**
     * @var int 类型id
     */
    protected int $taskType = 0;

    /**
     * @var AccountLogService 数据同步服务
     */
    private AccountLogService $service;

    public function __construct(int $mediaId = 0, int $taskType = 0)
    {
        $this->mediaId = $mediaId;
        $this->taskType = $taskType;
    }

    /**
     * 获取对象.
     */
    public function getObject(): bool
    {
        $className = CdcEnum::getServiceClassNames($this->mediaId, 'sync', $this->taskType);
        if (! $className) {
            return self::setError('获取数据同步服务失败');
        }
        $this->service = new $className();
        return true;
    }

    /**
     * 获取列表.
     * @return mixed
     */
    public function getList(array $params = [])
    {
        return $this->service::getList($params);
    }

    /**
     * 运行修复.
     * @param MongoSyncModel $model 中间表模型
     */
    public function run(MongoSyncModel $model): bool
    {
        return $this->service->runStat($model);
    }

    /**
     * 添加中间表信息.
     * @param MongoReportModel $model 报表模型
     * @return mixed
     */
    public function addLog(MongoReportModel $model)
    {
        return $this->service::addLogByRecord($model);
    }

    public function delLog(MongoSyncModel $model, int $statDate = 0): bool
    {
        return $this->service::deleteLog($model, $statDate);
    }

    public function delSyncLog(array $params = [])
    {
        return $this->service->delSyncLog($params);
    }

    public function getInfo(array $params = [])
    {
        return $this->service::getInfo($params);
    }
}
